package com.huawei.java.main;

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Map<Integer,ArrayList<ClientNode>> map=new HashMap<>();
        ArrayList<ClientNode> clientList=new ArrayList();
        ArrayList<EdgeNode> edgeNodeList=new ArrayList();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("data/demand.csv")));
        BufferedReader br2 = new BufferedReader(new InputStreamReader(new FileInputStream("data/site_bandwidth.csv")));
        BufferedReader br3 = new BufferedReader(new InputStreamReader(new FileInputStream("data/qos.csv")));
        FileInputStream reader=new FileInputStream("data/config.ini");
        Properties pro=new Properties();
        pro.load(reader);
        String qos_constraint = pro.getProperty("qos_constraint");
        int config = Integer.parseInt(qos_constraint);

        String line;
        String []TopInfo=br.readLine().split(",");

        for(int i=1;i<TopInfo.length;i++) {
            ClientNode clientNode = new ClientNode(TopInfo[i],0);
            clientList.add(clientNode);
        }
        int count=1;
        while ( (line = br.readLine()) != null ) {
            String[] info = line.split(",");
            ArrayList<ClientNode> list=new ArrayList();
            for(int i=1;i<info.length;i++) {
                ClientNode clientNode = clientList.get(i - 1);
                clientNode.setBw(Integer.parseInt(info[i]));
                list.add(new ClientNode(clientNode.getName(),clientNode.getBw()));
            }
            map.put(count++,list);
        }

        br2.readLine();
        while((line=br2.readLine())!=null){
            String[] info = line.split(",");
            EdgeNode edgeNode = new EdgeNode(info[0],Integer.parseInt(info[1]));
            edgeNodeList.add(edgeNode);
        }

        //保存一份边缘节点的初始值
        int[] intialTotalBw=new int[edgeNodeList.size()];
        for (int i = 0; i <edgeNodeList.size() ; i++) {
             intialTotalBw[i]=edgeNodeList.get(i).getTotalBW();
        }

        int[][]time=new int[edgeNodeList.size()][clientList.size()]; //用来存放客户和边缘的时延
        br3.readLine();
        int column=0;
        while((line=br3.readLine())!=null){
            String[] info = line.split(",");
            for(int i=1;i<info.length;i++) {
                time[column][i-1]=Integer.parseInt(info[i]);
            }
            column++;
        }


        FileOutputStream fos=new FileOutputStream(new File("output/solution.txt"));
        int index;//表示边缘节点的下标
        EdgeNode edgeNode;
//        for (int i = 1; i <=map.size(); i++) {
//            System.out.println("第"+i+"个时刻");
//            ArrayList<ClientNode> clientNodes = map.get(i); //第i组客户节点
//            //对第一组数组处理完之后，我们要把边缘节点的总带宽大小调整回初始时刻
//            for (int j = 0; j < edgeNodeList.size(); j++) {
//                edgeNodeList.get(j).setTotalBW(intialTotalBw[j]);
//            }
//            for (int j = 0; j <clientNodes.size() ; j++) {
//                index=0;
//                ClientNode clientNode = clientNodes.get(j);//第j个客户
//                if(clientNode.getBw()!=0) {
//                    String res=clientNode.getName()+":";
//                    while (clientNode.getBw()!=0) { //只有当客户节点的带宽变成0，说明分配完成才退出
//                        while (time[index][j] >= config||edgeNodeList.get(index).getTotalBW()==0) {
//                            index++; //找到一个边缘节点，和该客户节点的时延小于400
//                        }
//                        edgeNode = edgeNodeList.get(index); //第index个边缘节点
//                        if (edgeNodeList.get(index).getTotalBW() < clientNode.getBw()) { //如果剩余带宽小于客户带宽
//                            clientNode.setBw(clientNode.getBw() - edgeNodeList.get(index).getTotalBW());
//                            res+= "<" + edgeNode.getName() + "," + edgeNode.getTotalBW() + ">,";
//                            edgeNode.setTotalBW(0);
//                        }else{
//                            if(j==clientNodes.size()-1&&i==map.size()){
//                                res += "<" + edgeNode.getName() + "," + clientNode.getBw() + ">";
//                            }else {
//                                res += "<" + edgeNode.getName() + "," + clientNode.getBw() + ">\n";
//                                edgeNode.setTotalBW(edgeNode.getTotalBW()-clientNode.getBw());
//                            }
//                                byte[] b = res.getBytes();
//                                for (int k = 0; k < b.length; k++) {
//                                    fos.write(b[k]);
//                                }
//                                break;
//                        }
//                        index++;
//                    }
//                }else{
//                    String res="";
//                    if(j==clientNodes.size()-1&&i==map.size()) {
//                        res=clientNode.getName()+":";
//
//                    }else {
//                        res = clientNode.getName() + ":\n";
//                    }
//                    byte[] b = res.getBytes();
//                    for (int k = 0; k < b.length; k++) {
//                        fos.write(b[k]);
//                    }
//                }
//            }
//
//        }
        for (int i = 1; i <=map.size(); i++) {
            System.out.println("第"+i+"个时刻");
            ArrayList<ClientNode> clientNodes = map.get(i); //第i组客户节点
            //对第一组数组处理完之后，我们要把边缘节点的总带宽大小调整回初始时刻
            for (int j = 0; j < edgeNodeList.size(); j++) {
                edgeNodeList.get(j).setTotalBW(intialTotalBw[j]);
            }
            for (int j = 0; j <clientNodes.size() ; j++) {
                index=0;
                ClientNode clientNode = clientNodes.get(j);//第j个客户
                if(clientNode.getBw()!=0) {
                    String res=clientNode.getName()+":";
                    while (clientNode.getBw()!=0) { //只有当客户节点的带宽变成0，说明分配完成才退出
//                        while (time[index][j] >= config||edgeNodeList.get(index).getTotalBW()==0) {
//                            index++; //找到一个边缘节点，和该客户节点的时延小于400
//                        }
//                        edgeNode = edgeNodeList.get(index); //第index个边缘节点
//                        if (edgeNodeList.get(index).getTotalBW() < clientNode.getBw()) { //如果剩余带宽小于客户带宽
//                            clientNode.setBw(clientNode.getBw() - edgeNodeList.get(index).getTotalBW());
//                            res+= "<" + edgeNode.getName() + "," + edgeNode.getTotalBW() + ">,";
//                            edgeNode.setTotalBW(0);
//                        }else{
//                            if(j==clientNodes.size()-1&&i==map.size()){
//                                res += "<" + edgeNode.getName() + "," + clientNode.getBw() + ">";
//                            }else {
//                                res += "<" + edgeNode.getName() + "," + clientNode.getBw() + ">\n";
//                                edgeNode.setTotalBW(edgeNode.getTotalBW()-clientNode.getBw());
//                            }
//                            byte[] b = res.getBytes();
//                            for (int k = 0; k < b.length; k++) {
//                                fos.write(b[k]);
//                            }
//                            break;
//                        }
//                        index++;
                        List<Integer> IndexList=new ArrayList();
                        while(index<edgeNodeList.size()&&edgeNodeList.get(index).getTotalBW()!=0) {
                            if (time[index][j] < config) {
                                IndexList.add(index);  //存放满足时延条件的边缘节点
                            }
                            index++;
                        }
//                        for (int k = 0; k <IndexList.size() ; k++) {
//                            int avlBW=clientNode.getBw()/(IndexList.size()-1);
//                            EdgeNode edgeNode1=edgeNodeList.get((IndexList.get(k)));
//                            if(avlBW>edgeNode1.getTotalBW()){
//
//                            }
//                        }
                    }
                }else{
                    String res="";
                    if(j==clientNodes.size()-1&&i==map.size()) {
                        res=clientNode.getName()+":";

                    }else {
                        res = clientNode.getName() + ":\n";
                    }
                    byte[] b = res.getBytes();
                    for (int k = 0; k < b.length; k++) {
                        fos.write(b[k]);
                    }
                }
            }

        }
        fos.close();
        br.close();
        br2.close();
        br3.close();
    }

}

